#include "GTestEngine.h"
#include "..\..\Common\C3DModel.h"

CGTestEngine::CGTestEngine(void)
{
}

CGTestEngine::~CGTestEngine(void)
{
}

void CGTestEngine::OnTick( int elapsed )
{
	if(m_scene!=NULL)
		m_scene->m_camera.Tick(elapsed);
}

void CGTestEngine::OnIdle()
{
	glPushMatrix();
	glClear(GL_COLOR_BUFFER_BIT);
	
	m_scene->m_camera.Translate();
	
	glColor3f(0.7, 0.5, 0.8);
	//glRectf(1.0, 1.0, 3.0, 2.0);

	
	m_scene->Draw();
	

	SDL_GL_SwapBuffers();
	glPopMatrix();
}

void CGTestEngine::Init()
{
	glLoadIdentity( );
	//gluOrtho2D(0.0, 4.0, 0.0, 3.0);
	//glRotatef(30.0,0.0f,1.0f,0);
	gluPerspective(90.0f,4/3,0.5f,200.0f);
	m_scene = new CGScene();

	C3DModel* md = new C3DModel();
	md->Create(4);
	md->SetTriangle(0,C3DTriangle(
		C3DPoint(0.0f,0.0f,0.0f),
		C3DPoint(1.0f,0.0f,0.0f),
		C3DPoint(0.0f,1.0f,0.0f)));
	md->SetTriangle(1,C3DTriangle(
		C3DPoint(0.0f,0.0f,0.0f),
		C3DPoint(0.0f,0.0f,1.0f),
		C3DPoint(0.0f,1.0f,0.0f)));
	md->SetTriangle(2,C3DTriangle(
		C3DPoint(0.0f,0.0f,0.0f),
		C3DPoint(0.0f,0.0f,1.0f),
		C3DPoint(1.0f,0.0f,0.0f)));
	md->SetTriangle(3,C3DTriangle(
		C3DPoint(0.0f,1.0f,0.0f),
		C3DPoint(0.0f,0.0f,1.0f),
		C3DPoint(1.0f,0.0f,0.0f)));
	md->SetPosition(0.0f,0.0f,-3.0f);
	
	m_scene->m_object.AddObject(md);
	md = new C3DModel();
	md->LoadModel("model.3ds");
	md->SetPosition(0,0,-20.0f);
	m_scene->m_object.AddObject(md);
}

void CGTestEngine::Deinit()
{
	if(m_scene!=NULL)
		delete m_scene;
	m_scene = NULL;
}

void CGTestEngine::OnKeyDown( SDL_KeyboardEvent kevent )
{
	switch(kevent.keysym.sym)
	{
	case SDLK_LEFT:
		m_scene->m_camera.SetSpeedX(CAMERA_SPEED);
		break;
	case SDLK_RIGHT:
		m_scene->m_camera.SetSpeedX(-CAMERA_SPEED);
		break;
	case SDLK_UP:
		m_scene->m_camera.SetSpeedY(-CAMERA_SPEED);
		break;
	case SDLK_DOWN:
		m_scene->m_camera.SetSpeedY(CAMERA_SPEED);
		break;
	}
}

void CGTestEngine::OnKeyUp( SDL_KeyboardEvent kevent )
{
	switch(kevent.keysym.sym)
	{
	case SDLK_LEFT:
		m_scene->m_camera.SetSpeedX(0);
		break;
	case SDLK_RIGHT:
		m_scene->m_camera.SetSpeedX(0);
		break;
	case SDLK_UP:
		m_scene->m_camera.SetSpeedY(0);
		break;
	case SDLK_DOWN:
		m_scene->m_camera.SetSpeedY(0);
		break;
	}
}